<?php


namespace app\backend\modules\content\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\content\services\ProductService;
use app\common\exceptions\BizException;
use app\common\models\Product;
use app\common\models\ProductChannel;
use app\common\models\ProductProperty;
use Illuminate\Support\Facades\DB;

class ProductController extends BaseController
{
    public function index()
    {
        return view("backend.content.product.index");
    }
    public function getList(){
        $search = request()->search;
        $list = (new ProductService())->getList($search);
        return $this->successJson('',$list);
    }

    public function changeStatus()
    {
        $id = request()->id;
        $model = Product::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            return $this->errorJson('未找到数据');
        }
        $status=Product::STATUS_YES;
        if($model->status==Product::STATUS_YES){
            $status= Product::STATUS_NO;
        }
        $model->status = $status;
        $model->save();
        return $this->successJson();
    }

    public function changePropertyStatus()
    {
        $id = request()->id;
        $type = request()->type;
        $model = Product::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            return $this->errorJson('未找到数据');
        }
        switch ($type){
            case 'is_new':
                $status=Product::STATUS_YES;
                if($model->is_new==Product::STATUS_YES){
                    $status= Product::STATUS_NO;
                }
                $model->is_new = $status;
                break;
            case 'is_recommend':
                $status=Product::STATUS_YES;
                if($model->is_recommend==Product::STATUS_YES){
                    $status= Product::STATUS_NO;
                }
                $model->is_recommend = $status;
                break;
            case 'is_hot':
                $status=Product::STATUS_YES;
                if($model->is_hot==Product::STATUS_YES){
                    $status= Product::STATUS_NO;
                }
                $model->is_hot = $status;
                break;
        }
        $model->save();
        return $this->successJson();
    }

    public function detail()
    {
        return view("backend.content.product.detail");
    }

    public function getDetail()
    {
        $id = intval(request()->id);
        $res = [];
        $images_url = [];
        if($id){
            $model = Product::site()->select()
                ->where('id',$id)
                ->first();
            if (empty($model)) {
                return $this->errorJson('未找到数据');
            }
            $model->thumb = fmt_media_url($model->thumb);
            $model->images = json_decode($model->images,true);
            foreach ($model->images as $k=>$v){
                $images_url[] = fmt_media_url($v);
            }
            $model->images_url = $images_url;
            $res['info'] = $model;
            $channel_list = ProductChannel::site()->select('id','name','url')
                ->where('product_id',$id)
                ->get();
            if($channel_list){
                $res['channel_list'] = $channel_list;
            }
            $property_list = ProductProperty::site()->select('id','name','value')
                ->where('product_id',$id)
                ->get();
            if($property_list){
                $res['property_list'] = $property_list;
            }
        }
        return $this->successJson('',$res);
    }

    public function create()
    {
        $data = request()->params;
        $channelData = request()->channel;
        $propertyData = request()->property;
        $data['site_id'] = \YunCms::siteId();
        $data['display_order'] = intval($data['display_order']);
        $data['images'] = json_encode($data['images'],true);
        $model = new Product();
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            DB::beginTransaction();
            if(!$model->save()){
                DB::rollBack();
                throw new BizException('保存失败');
            }
            foreach ($channelData as $k => $v) {
                $channelData[$k]['product_id'] = $model['id'];
                $channelData[$k]['site_id'] = $data['site_id'];
                $channelData[$k]['created_at'] = time();
            }
            ProductChannel::insert($channelData);
            foreach ($propertyData as $k => $v) {
                $propertyData[$k]['product_id'] = $model['id'];
                $propertyData[$k]['site_id'] = $data['site_id'];
                $propertyData[$k]['created_at'] = time();
            }
            ProductProperty::insert($propertyData);
            DB::commit();
        }
        return $this->successJson('保存成功');
    }

    public function edit()
    {
        $data = request()->params;
        $id = $data['id'];
        $channelData = request()->channel;
        $propertyData = request()->property;
        $data['site_id'] = \YunCms::siteId();
        $data['display_order'] = intval($data['display_order']);
        $productService = new ProductService();
        $model = Product::find($id);
        if (empty($model)) {
            return $this->errorJson('未找到数据');
        }
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $productService->saveData($channelData,$data,$model);
            $productService->savePropertyData($data,$propertyData);
        }

        return $this->successJson('保存成功');
    }

    public function delete()
    {
        $id = request()->id;
        $model = Product::find($id);
        if(!$model){
            return $this->errorJson('未找到数据');
        }
        $model->delete();
        return $this->successJson();
    }


}
